﻿using System;
using System.ComponentModel;
using System.Linq;
using GSPMigratorCore.Misc;
using GSPMigratorCore.Helper;
using Microsoft.SharePoint;

namespace GSPMigratorCore.GSPInterface
{
    public class GSPInterface : BaseClass
    {
        public bool ValidateData(string input, out string errMsg)
        {            
            bool isValid = false;
            errMsg = string.Empty;
            try
            {
                isValid = true;
            }
            catch (GSPException GSPex)
            { 
                
            }
            catch (Exception ex)
            {
                //TODO: logging
                
                isValid = false;
                errMsg = "Error found at where?";
            }
            return isValid;
        }

        public bool ProcessData(string SPDocLibFullPath, string SPURL, string input, string replaceWith, BackgroundWorker bgw, out int currentLine)
        {
            bool success = false;
            try
            {
                string[] delimiter = { GSPConstants.NEWLineDelimiter };
                string[] test = input.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                int maxLine = test.Count();
                currentLine = 0;
                SPFolder oFolder;

                for (int i = 0; i < maxLine; i++)
                {
                    if (currentLine == maxLine) //end of file
                        goto label_1801;

                    if (test[i].StartsWith("DIR: ")) //directory entry from migration files
                    {
                        oFolder = FolderCreationHelper.Create(SPDocLibFullPath, SPURL, test[i], replaceWith);
                        currentLine = i;
                        int j = i + 1;
                        if (j != maxLine)
                        {
                            while (ValidNextLine(test[j]))
                            {
                                string[] temp = test[j].Split(':');
                                SetPermissions(SPURL, temp[0].Trim(), temp[1].Trim(), oFolder);
                                j++;
                                currentLine = j;

                                double progressPercent2 = (currentLine / double.Parse(maxLine.ToString())) * 100;
                                int pg2 = int.Parse(Math.Round(progressPercent2, MidpointRounding.ToEven).ToString());
                                bgw.ReportProgress(pg2);

                                if (j == maxLine)
                                {
                                    goto label_1801;
                                }
                            }
                        }
                    }
                    //currentLine++;
                }
            label_1801:
                success = true;
                //TODO: LOG
            }
            catch (Exception ex)
            {
                //TODO: LOG
                success = false;
                throw ex;
            }
            return success;
        }

        internal bool SetPermissions(string SPURL, string entity, string permissions, SPFolder oFolder)
        {
            bool success = false;
            SPRoleType oRoleType = new SPRoleType();
            int permissionType = -1;

            if (permissions.Contains(GSPConstants.NetworkDrive1601.Permissions.AllowFullControl))
            {
                permissionType = 1;
            }
            else if (permissions.Contains(GSPConstants.NetworkDrive1601.Permissions.AllowModify))
            {
                permissionType = 2;
            }
            else if (permissions.Contains(GSPConstants.NetworkDrive1601.Permissions.AllowReadAndExecute))
            {
                permissionType = 3;
            }


            switch (permissionType)
            {
                case 1:
                    oRoleType = SPRoleType.Administrator;
                    break;
                case 2:
                    oRoleType = SPRoleType.Contributor;
                    break;
                case 3:
                    oRoleType = SPRoleType.Contributor;
                    break;

                default:
                    oRoleType = SPRoleType.Reader;
                    break;
            }
            string errMsg = SPPermissionHelper.SetItemPermission(SPURL, oRoleType, oFolder, entity);
            if (string.IsNullOrEmpty(errMsg))
                return success;
            else
            {
                throw new Exception(errMsg);
            }
        }

        internal bool ValidNextLine(string entry)
        {
            bool isValid = false;
            if (entry.StartsWith("DIR: "))
                isValid = false;
            else if (entry.Contains(':'))
                isValid = true;
            return isValid;
        }
    }
}
